package id.khairizalnetani.tugasakhir;

import id.khairizalnetani.tugasakhir.entity.Laporan;
import id.khairizalnetani.tugasakhir.entity.Siswa;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import roboguice.activity.RoboActivity;
import roboguice.inject.InjectView;

public class LihatLaporan extends RoboActivity implements OnClickListener
{
	// inisialisasi objek layout
	@InjectView(R.id.hal3TVSkadronLihatLaporan)
	TextView					tvSkadron;
	@InjectView(R.id.hal3TvPelaporLihatLaporan)
	TextView					tvPelapor;
	@InjectView(R.id.hal3TVidLihatLaporan)
	TextView					tvIdPelapor;
	@InjectView(R.id.hal3TVTanggalLihatLaporan)
	TextView					tvTanggal;

	@InjectView(R.id.tvHal3JenisApelLihatLaporan)
	TextView					tvJenisApel;
	@InjectView(R.id.tvhal3jumlahsiswaLihatLaporan)
	TextView					tvJumlahSiswa;
	@InjectView(R.id.tvHal3KurangLihatLaporan)
	TextView					tvKurang;
	@InjectView(R.id.tvHal3HadirLihatLaporan)
	TextView					tvHadir;
	@InjectView(R.id.tvHal3DinasDalamLihatLaporan)
	TextView					tvDinasDalam;
	@InjectView(R.id.tvHal3DinasLuarLihatLaporan)
	TextView					tvDinasLuar;
	@InjectView(R.id.tvHal3SakitLihatLaporan)
	TextView					tvSakit;
	@InjectView(R.id.tvHal3BerobatLihatLaporan)
	TextView					tvBerobat;
	@InjectView(R.id.tvHal3IzinLihatLaporan)
	TextView					tvIzin;
	@InjectView(R.id.tvHal3TanpaKeteranganLihatLaporan)
	TextView					tvTanpaKeterangan;

	@InjectView(R.id.btnKembaliLihatLaporan)
	Button						btnKembali;

	private ProgressDialog		pDialog;
	private DBAdapter			db;

	private Siswa				siswa;
	private Laporan				laporan;
	private int					idSiswa;
	private long				idReport;

	private int					jmlDD, jmlDL, jmlSakit, jmlBerobat, jmlIzin, jmlTk;

	private final int			TXT_APEL_PAGI		= 0;
	private final int			TXT_APEL_SIANG		= 1;
	private final int			TXT_APEL_MALAM		= 2;

	private static final int	TXT_DINASDALAM		= 0;
	private static final int	TXT_DINASLUAR		= 1;
	private static final int	TXT_SAKIT			= 2;
	private static final int	TXT_BEROBAT			= 3;
	private static final int	TXT_IZIN			= 4;
	private static final int	TXT_TANPAKETERANGAN	= 5;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lihat_laporan);
		btnKembali.setOnClickListener(this);

		db = new DBAdapter(this);
		db.open();

		Bundle bundle = getIntent().getExtras();
		if (bundle != null)
		{
			Utils.TRACE("id siswa " + bundle.getInt(Halaman1.TAG_ID));
			idSiswa = bundle.getInt(Halaman1.TAG_ID);
			idReport = bundle.getLong("id_report");
		}

		new AsyncTaskLoadSiswa().execute();
	}

	public void setTampilan()
	{
		String txtFlight = null;
		String txtSkadron = null;
		switch (siswa.getFlight())
		{
			case 1:
				txtFlight = "Flight I";
				break;
			case 2:
				txtFlight = "Flight II";
				break;
			case 3:
				txtFlight = "Flight III";
				break;
			case 4:
				txtFlight = "Flight IV";
				break;
		}
		switch (siswa.getSkadron())
		{
			case 2:
				txtSkadron = "Skadron II";
				break;
			case 3:
				txtSkadron = "Skadron III";
				break;
			case 4:
				txtSkadron = "Skadron IV";
				break;
		}

		tvSkadron.setText(txtFlight + " " + txtSkadron);
		tvPelapor.setText("Nama : " + siswa.getNama());
		tvIdPelapor.setText("ID : " + siswa.getId());
		tvTanggal.setText(laporan.getTanggal());
		tvJenisApel.setText(getJenisApel(laporan.getJenis_apel()));
		tvJumlahSiswa.setText(getValueOf(laporan.getJumlah()));
		tvKurang.setText(getValueOf(laporan.getJumlah()));
		tvHadir.setText(getValueOf(laporan.getHadir()));
		tvDinasDalam.setText(getValueOf(jmlDD));
		tvDinasLuar.setText(getValueOf(jmlDL));
		tvSakit.setText(getValueOf(jmlSakit));
		tvBerobat.setText(getValueOf(jmlBerobat));
		tvIzin.setText(getValueOf(jmlIzin));
		tvTanpaKeterangan.setText(getValueOf(jmlTk));

	}

	private String getValueOf(int a)
	{
		return String.valueOf(a);
	}

	private String getJenisApel(int jenisApel)
	{
		switch (jenisApel)
		{
			case TXT_APEL_PAGI:
				return "Apel Pagi";

			case TXT_APEL_SIANG:
				return "Apel Siang";

			case TXT_APEL_MALAM:
				return "Apel Malam";
		}
		return null;
	}

	private class AsyncTaskLoadSiswa extends AsyncTask<Void, Void, Void>
	{
		@Override
		protected Void doInBackground(Void... params)
		{
			// TODO Auto-generated method stub
			siswa = db.getSiswaById(idSiswa);
			laporan = db.getReportById(idReport);

			jmlDD = db.getCountKeteranganByJenisAndIdReport(TXT_DINASDALAM, idReport);
			jmlDL = db.getCountKeteranganByJenisAndIdReport(TXT_DINASLUAR, idReport);
			jmlSakit = db.getCountKeteranganByJenisAndIdReport(TXT_SAKIT, idReport);
			jmlBerobat = db.getCountKeteranganByJenisAndIdReport(TXT_BEROBAT, idReport);
			jmlIzin = db.getCountKeteranganByJenisAndIdReport(TXT_IZIN, idReport);
			jmlTk = db.getCountKeteranganByJenisAndIdReport(TXT_TANPAKETERANGAN, idReport);
			return null;
		}

		@Override
		protected void onPostExecute(Void result)
		{
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			pDialog.dismiss();
			setTampilan();
		}

		@Override
		protected void onPreExecute()
		{
			// TODO Auto-generated method stub
			super.onPreExecute();

			pDialog = new ProgressDialog(LihatLaporan.this);
			pDialog.setCancelable(true);
			pDialog.setMessage("Loading...");
			pDialog.show();
		}
	}

	@Override
	public void onClick(View v)
	{
		if (v == btnKembali)
		{
			// pindah ke selamat datang

			Bundle b = new Bundle();
			b.putInt(Halaman1.TAG_ID, siswa.getId());

			Intent i = new Intent(LihatLaporan.this, SelamatDatangActivity.class);
			i.putExtras(b);
			startActivity(i);
			db.close();
			finish();
		}

	}

}
